{% extends 'base.html' %}

{% block title %}用户登录页面{% endblock %}


{% block headerjs %}

    <script type="text/javascript" src="/static/md5-min.js"></script>

{% endblock %}


{% block main %}


     <div class="login-body">
        <div class="login-card">
            <div class="login-top">
                <h3>新用户登录</h3>
                <h5>尊敬的用户，欢迎您回来！</h5>
            </div>
            <div class="login-bottom">
               <form method="post" action="/user/login/" onsubmit="return login();">
                    {% csrf_token %}
                    <div class="login-input-box">

                    <input type="text" id="account" name="account" value="" autocomplete="off" class="login-admin active" placeholder="邮箱登录">
                    <span style="color:red;" id="aSpan"></span>

                    <input type="password" id="password" name="password" value="" autocomplete="off"  class="login-password">
                    <span style="color:red;" id="pSpan"></span>

                    <input type="text" style="height:36px;width: 100px;" autocomplete="off"  id="code" onblur="checkCode(this.value)"><img src="/user/loadCode/" onclick="changeCode(this)"><span style="color:red;" id="cSpan"></span>

                    <input type="hidden" name="redirect" value="{{ redirect }}">
                    <input type="hidden" name="cartitems" value="{{ cartitems }}">

                    <button class="login-btn" style="color: white;text-decoration: none;cursor: pointer">登录</button>
                </div>
               </form>
                <div class="login-img">
                    <img src="/static/login_05.png" alt="">
                </div>
            </div>
        </div>
    </div>




{% endblock %}


{% block footerjs %}

    
    
    <script>
        function isEmail(str){
            var reg = /^[a-zA-Z0-9_-]{6,}@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
            return reg.test(str);
        }

        function login(){
            //获取输入框的值
            var account = $('#account').val();
            var password = $('#password').val();

            var flag = true;

            //简单校验
            if(account.length<6||!isEmail(account)){
                $('#aSpan').text('邮箱格式不正确！');
                flag = false;
            }

            if(password.length<6){
                $('#pSpan').text('密码长度不能小于六位');
                flag = false;
            }

            var code = $('#code').val();
            var cflag = checkCode(code);
            if(!cflag){
                flag = false
            }

            if(flag){
                var hex_pwd = hex_md5(password);
                $('#password').val(hex_pwd);

            }else{
                return false;
            }



        }


        //切换验证码
        function changeCode(obj){
            $(obj).attr('src','/user/loadCode/?r='+new Date().getTime())
        }


       //校验验证码是否正确
        function checkCode(inputCode) {
            var flag = false;
            $.ajax({
                url:'/user/checkCode/',
                type:'GET',
                data:{'code':inputCode},
                async:false,
                success:function (result) {
                    var cflag = result.flag;
                    if(cflag){
                        flag = true;
                        $('#cSpan').html('√');
                    }else{
                        $('#cSpan').html('×');
                    }
                }

            });


            return flag;


        }
    </script>



{% endblock %}